/**
 *
 */
package org.jprobe.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * RegEx and Text manipulation methods
 *
 * @author Hussain Al-Mutawa
 * @version 1.0
 * @since 1.0
 */
public final class RegExUtils {

	/** prevent initialization of this factory class **/
	private RegExUtils(){super();}

	/** empty string **/
	public transient static final String EMPTY = "";

	/** <code>.java</code> or <code>.class</code> file pattern matcher, can be used with group (3) **/
	public transient static final String CLASS_FILE = "^((.*/)*)(([a-zA-Z]+[0-9]*[a-zA-Z]*)*)(\\.(((c|C)(l|L)(a|A)(s|S)(S|s))|((J|j)(A|a)(V|v)(a|A)))$)";

	/** removes the extension from file path pattern matcher, can be used with group (1) **/
	public transient static final String EXTENSION = "^(((.*/)*)(.*))(\\.(.*)$)";

	/** extract the package name from java source file, can be used with group 2 **/
	public transient static final String PACKAGE = ".*package\\s+((\\S*))\\s*\\;.*";
	/** method name extractor, used with groups (1,3) together **/
	public transient static final String METHOD_NAME_REGEX = "^(.*\\s)*(.*\\.)*((.*)$)";

//	public static transient final Map<Character,String>PRIMITIVE_ARRAY_ENCODE=new HashMap<Character,String>();;
//	static {
//		PRIMITIVE_ARRAY_ENCODE.put('I',"int");
//		PRIMITIVE_ARRAY_ENCODE.put('Z',"boolean");
//		PRIMITIVE_ARRAY_ENCODE.put('C',"char");
//		PRIMITIVE_ARRAY_ENCODE.put('D',"double");
//		PRIMITIVE_ARRAY_ENCODE.put('S',"short");
//		PRIMITIVE_ARRAY_ENCODE.put('F',"float");
//		PRIMITIVE_ARRAY_ENCODE.put('J',"long");
//		PRIMITIVE_ARRAY_ENCODE.put('B',"byte");
//		PRIMITIVE_ARRAY_ENCODE.put('L',"");
//	}

	/**
	 * extract a text from string by applying the regular expression pattern
	 * @param text the text to be searched
	 * @param regex the regular expression
	 * @param group the index of the matched group by the pattern matcher
	 * @return the extracted text, otherwise if not available returns empty string {@link RegExUtils#EMPTY}
	 */
	public static String extract(final String text,final String regex,final int group){
		final Pattern p = Pattern.compile(regex);
		final Matcher m = p.matcher(text.replaceAll("\\\\", "/"));

		String result;
		if (m.find()) {
//			System.out.println("["+text+"]=================");
//			for(int i=0;i<m.groupCount();i++){
//				System.out.println(m.group(i));
//			}
//			System.out.println("======================================================");
		    result = m.group(group);
		}else{
			result = RegExUtils.EMPTY;
		}
		return result;
	}

	public static boolean equalsIgnoreCase(final String st1, final String st2) {
		return st1.toLowerCase().equals(st2.toLowerCase());
	}
}
